package cn.tedu.jdbc;

import java.sql.*;

public class JDBCUtils {
    //获取和数据库的连接
    static public Connection getconnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///abc?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root105");
        return conn;
    }

    /**
     * 释放资源
     *
     * @param rs   结果集
     * @param ps   传输器
     * @param conn 数据库的连接
     */
    static public void close(ResultSet rs, PreparedStatement ps, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                System.out.println("执行失败");//上线后
                throwables.printStackTrace();//调试阶段
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
